package org.lagstains
{
	import org.flixel.*;
	
	/**
	 * ...
	 * @author ...
	 */
	public class Player extends FlxSprite
	{
		[Embed(source = "../../data/images/Player.png")] private var PlayerSprite:Class;

		private var trackHeight:int;

		public function Player(x:int, y:int, trackHeight:int) 
		{
			super(x, y);
			loadGraphic(PlayerSprite, true, true, 16, 20);	
			this.scale.x = 1.5;
			this.scale.y = 1.5;
			
			this.trackHeight = trackHeight;
			
		}
		
		override public function update():void
		{
			handleInput();
			super.update();
		}
		
		private function handleInput():void
		{	
			
			// move the player		
			if (FlxG.keys.LEFT)
			{
				this.velocity.x = -200;
			}
			else if (FlxG.keys.RIGHT)
			{
				this.facing = FlxSprite.RIGHT;
				this.velocity.x = 200;
			} else {
				this.velocity.x = 0;				
			}
			
			if (FlxG.keys.UP)
			{
				this.velocity.y = -200;
			}
			else if (FlxG.keys.DOWN)
			{
				this.velocity.y = 200;
			}
			else
			{
				this.velocity.y = 0;
			}
			
			// keep the player on the screen
			if(this.x <= 0)
				this.x = 0;
				
			if(this.y <= (FlxG.height - this.trackHeight))
				this.y = (FlxG.height - this.trackHeight);
				
			if(this.x >= FlxG.width-20)
				this.x = FlxG.width-20;
				
			if(this.y >= FlxG.height-20)
				this.y = FlxG.height-20;
			
			

		}
		
	}

}